// const app = require('express')()


//导入数据库操作模块
const db = require("../../db/index")


/**
 * 获取学习资料列表
 * @param {*} req 
 * @param {*} res 
 */
exports.getStudyDataList = async (req, res) => {
    console.log('获取学习资料列表')
    const sql = `SELECT data.id,data.name,tags.tag_name AS tags,data.description,data.data_url,data.flag FROM xks_study_data AS data, xks_tags AS tags WHERE tags.tag_type = "学习资料" AND data.id=tags.c_id `;
    await db.query(sql, (err, results) => {
        if (err) {
            return res.send({
                code: 505,
                msg: '获取失败，发送了错误' + err.message,
                data: {}
            })
        }

        if (!results.length > 0) {
            return res.send({
                code: 400,
                msg: '抱歉，没有找到你想要的内容',
                data: {}
            })
        }

        return res.send({
            code: 200,
            msg: '成功',
            data: {
                list: results
            }
        })
    })
}




/**
 * 根据资料名称获取资料列表（模糊匹配）
 * @param {*} req 
 * @param {*} res 
 */
exports.getStudyDataListByName = (req, res) => {
    // console.log('获取学习视频列表'+req.ip)
    const name = req.query.name
    // console.log('title' + title)
    sql = `SELECT data.id,data.name,tags.tag_name AS tags,data.description,data.data_url,data.flag FROM xks_study_data AS data, xks_tags AS tags WHERE tags.tag_type = "学习资料" AND data.name LIKE concat('%',?, '%') AND data.id=tags.c_id `;
    db.query(sql, name, (err, results) => {
        if (err) {
            return res.send({
                code: 505,
                msg: '发送了错误' + err.message,
                data: {}
            })
        }

        if (!results.length > 0) {
            return res.send({
                code: 400,
                msg: '抱歉，没有找到你想要的内容',
                data: {}
            })
        }

        return res.send({
            code: 200,
            msg: '成功',
            data: {
                list: results
            }
        })
    })
}
